
(function() {
    CAAT.PlayActor = function() {
        CAAT.PlayActor.superclass.constructor.call(this);
        return (this);
    }

    var self;
    var director;

    var spriteDude;
    var showHP;

    var jumpPb;
    var pushBackPb;

	var baseYPos;

    var resetState = function() {
        self.isRun = false;
        self.isJump = false;
        self.isStand = false;
        self.isPushBack = false;
    }

    CAAT.PlayActor.prototype = {
        isRun : false,
        isJump : false,
        isStand : false,
        isPushBack : false,
        velocity : 0,
        hp : 3,
        currentHP:  3,
        //currentHP: PLAYER_HP,
        
        create : function(direct, velo, baseY) {
            self = this;
            director = direct;

            var reset = function(spriteImage, time) {
                spriteImage.playAnimation("run");
            };

            spriteDude = new CAAT.Foundation.SpriteImage().
                initialize(director.getImage("dude"), 21, 7).
                addAnimation("stand",   [123,124,125, 126,127,128,129,130,131,132, 133,134,135,136,137,138,139, 140,141,142,143,144], 100).
                addAnimation("fall",    [0,1,2,3,4,5,6,7], 100, reset).
                addAnimation("wall_ud", [74,75,76, 77,78,79,80,81], 100).
                addAnimation("wall_lr", [82,83, 84,85,86,87,88,89], 100).
                addAnimation("tidy",    [42,43,44,45,46,47,48, 49,50], 100, reset).
                addAnimation("die",     [68,69, 70,71,72,73], 100, reset).
                addAnimation("jump",    [95,94,93,92,91, 90], 100, reset).
                addAnimation("run_b",   [96,97, 98,99,100,101,102,103,104, 105,106,107,108,109,110,111, 112,113,114,115,116,117,118, 119,120,121,122], 30).
                addAnimation("run",     [122,121,120,119, 118,117,116,115,114,113,112, 111,110,109,108,107,106,105, 104,103,102,101,100,99,98, 97,96], 30).
                addAnimation("sad",     [26,27, 28,29,30,31,32,33], 100);
            
            showHP = new CAAT.Foundation.ActorContainer().setBounds(0, 0, 50, 50);
            showHP.paint = function(director,time){
                var ctx = director.ctx;
                    ctx.strokeRect(0, 20, 50, 3);
                    var percent = self.currentHP/self.hp;
                    if(percent>2/3) ctx.fillStyle = "#0F0";             
                    else if(percent>1/3) ctx.fillStyle = "#FF0";
                    else ctx.fillStyle = "#F00";
                    ctx.fillRect(0, 20, 50 * self.currentHP / self.hp, 3);
            }
            this.addChild(showHP);

            this.setBackgroundImage(spriteDude)
                .setScale(0.9,0.9);

            jumbPb = new CAAT.Behavior.PathBehavior()
                .setCycle(false);
            
            pushBackPb = new CAAT.Behavior.PathBehavior()
                .setCycle(false);

            this.reset(velo, baseY);
            return this;
        },

        reset : function(velo, baseY) {
            baseYpos = baseY;
            this.velocity = velo;
            self.currentHP = self.hp;
            resetState();
            self.isRun = true;
            this.playAnimation("run");
            return this;
        },

        jump : function(time) {
            console.log('check jump');
            console.log(self.isRun);
            if(self.isRun){
                resetState();
                self.isJump = true;
                self.playAnimation("jump");
                console.log('jump');

                jumbPb.setFrameTime(time, 100*7)
                    .setPath(
                        new CAAT.PathUtil.Path()
                            .beginPath(self.x,self.y)
                            .addCubicTo( self.x, self.y-50, self.x+0.7*self.velocity*100*0.5, self.y-100, self.x+0.7*self.velocity*100, self.y)
                            .endPath()
                    )
                    .addListener({
                        behaviorExpired : function(behavior, time, actor) {
                            actor.removeBehaviour(behavior);
                            resetState();
                            self.isRun = true;
                        }
                    });
                self.addBehavior(jumbPb);
            }
        },

        bePushBack : function(time) {
            self.currentHP -= 1;

            if(self.currentHP <=0){
                //self.removeChild(showHP);
                console.log("thua cmnr");
                if(self.isJump) {
                    self.emptyBehaviorList();
                    self.y = baseYpos;
                    console.log('handle jump');
                }
                self.x = 100;
            }
            else if(self.isRun || self.isJump){
                if(self.isJump) {
                    self.emptyBehaviorList();
                    self.y = baseYpos;
                    console.log('handle jump');
                }

                resetState();
                self.isPushBack = true;
                console.log('push back');

                self.playAnimation("die");

                pushBackPb.setFrameTime(time, 100*7)
                    .setPath(
                        new CAAT.PathUtil.Path().setLinear(self.x, self.y, self.x - self.velocity*100*3, self.y))
                    .addListener({
                        behaviorExpired : function(behavior, time, actor) {
                            actor.removeBehaviour(behavior);
                            resetState();
                            self.isRun = true;
                        }
                    });
                self.addBehavior(pushBackPb);
            }
        },

        setStop : function() {
            resetState();
            self.isStand = true;
        }
    }

    extend(CAAT.PlayActor, CAAT.ActorContainer);

})();